#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
void move(const int& n, const int& m, int& x, int &y, const int& direct){
int xx=x, yy=y;
switch(direct){
case 0:
yy--;
break;
case 1:
yy++;
break;
case 2:
xx--;
break;
case 3:
xx++;
}
if(xx<0 || xx>n-1 || yy<0 || yy>m-1) return;
else{
x=xx; y=yy;
return;
}
}
ll solution(int n, int m, int x, int y, vector<vector<int>> queries){
ll answer=0;
for(int tmp_x=0; tmp_x<n; ++tmp_x){
for(int tmp_y=0; tmp_y<m; ++tmp_y){
int xx=tmp_x, yy=tmp_y;
for(vector<int> query: queries){
for(int i=0; i<query[1]; ++i){
move(n, m, xx, yy, query[0]);
}
}
if(xx==x && yy==y) ++answer;;
}
}
return answer;
}
int main(void){
int n1=2, n2=2, m1=2, m2=5, x1=0, x2=0, y1=0, y2=1;
vector<vector<int>> queries1={
{2, 1},
{0, 1},
{1, 1},
{0, 1},
{2, 1}
};
vector<vector<int>> queries2={
{3, 1},
{2, 2},
{1, 1},
{2, 3},
{0, 1},
{2, 1}
};
cout<<"result1: "<<solution(n1, m1, x1, y1, queries1)<<endl;
cout<<"result2: "<<solution(n2, m2, x2, y2, queries2)<<endl;
return 0;
}